﻿@model IFDB.ViewModels.PartialViewModels.Interfaces.ITeamMatchListViewModel

@using (var mainAccordion = Html.JQueryUI().Begin(new Accordion().HeightStyle(HeightStyle.Content).Active(0).Collapsible(true, false)))
{
    foreach(var group in Model.TeamMatchList.GroupBy(m => m.MatchDate.Month))
    {
        using (mainAccordion.BeginPanel(group.Take(1).First().MatchDate.ToString("MMMM")))
        {
            foreach (var item in group)
            {
                <table class="TeamMatchList">
                    
                    <tr class="MatchHeader1">
                        <td colspan="2">
                            @Html.ActionLink(item.Competition.CompetitionName, "Index", "League", new { competitionId = item.Competition.CompetitionId }, null)
                        </td>
                        <td class="Date">
                            @string.Format("{0} {1}", Model.GetDayPrefix(item.MatchDate), item.MatchDate.ToString("MMMM yyyy h:mmtt"))
                        </td>
                    </tr>
                    <tr class="MatchHeader2">
                        <td colspan="3">
                            @Html.DisplayFor(modelItem => item.Venue.VenueName)
                        </td>
                    </tr>
                    <tr>
                        <td class="LeftTeam">
                            @Html.ActionLink(item.Team1.TeamNames.First().Name, "Index", "Team", new { teamId = item.Team1.TeamId }, null)
                        </td>
                        <td class="Score">
                            @Html.ActionLink(item.Team1FT + " - " + item.Team2FT, "Index", "Match", new { matchId = item.MatchId }, null)
                        </td>
                        <td class="RightTeam">
                            @Html.ActionLink(item.Team2.TeamNames.First().Name, "Index", "Team", new { teamId = item.Team2.TeamId }, null)
                        </td>
                    </tr>

                    @foreach (var goal in item.AppearanceEvents.OrderBy(g => g.Minute).ThenBy(g => g.Plus))
                    {
                            
                            if (goal.Team.TeamId == item.Team1Id  && goal.MatchEventType == IFDB.ResourceLibrary.MatchEventType.Goal)
                            {
                                <tr>
                                    <td class="HomeTeamScorer">
                                        @Html.ActionLink(goal.Person.Surname, "Index", "Person", new { personId = goal.Person.PersonId }, null)
                                        @GetMinutesDisplay(goal.Minute, goal.Plus)
                                    </td>
                                    <td colspan="2"></td>
                                </tr>
                            }

                            if (goal.TeamId == item.Team2Id && goal.MatchEventType == IFDB.ResourceLibrary.MatchEventType.OwnGoal)
                            {
                                <tr>
                                    <td class="HomeTeamScorer">
                                        @Html.ActionLink(goal.Person.Surname, "Index", "Person", new { personId = goal.Person.PersonId }, null)
                                        @GetMinutesDisplay(goal.Minute, goal.Plus)
                                        <span>o.g.</span>
                                    </td>
                                    <td colspan="2"></td>    
                                </tr>
                            }

                            if (goal.TeamId == item.Team2Id && goal.MatchEventType == IFDB.ResourceLibrary.MatchEventType.Goal)
                            {
                                <tr>
                                    <td colspan="2"></td>    
                                    <td class="AwayTeamScorer">
                                        @Html.ActionLink(goal.Person.Surname, "Index", "Person", new { personId = goal.Person.PersonId }, null)
                                        @GetMinutesDisplay(goal.Minute, goal.Plus)
                                    </td>
                                </tr>
                            }

                            if (goal.TeamId == item.Team1Id && goal.MatchEventType == IFDB.ResourceLibrary.MatchEventType.OwnGoal)
                            {
                                <tr>
                                    <td colspan="2"></td>    
                                    <td class="AwayTeamScorer">
                                        @Html.ActionLink(goal.Person.Surname, "Index", "Person", new { personId = goal.Person.PersonId }, null)
                                        @GetMinutesDisplay(goal.Minute, goal.Plus)
                                        <span>o.g.</span>
                                    </td>
                                </tr>
                            }
                    }
                </table>
            }
        }
    }
}

@functions
{
    private string GetMinutesDisplay(int? minute, int? plus)
    {
        if (minute == null)
            return string.Empty;

        if (plus == null)
            return string.Format("({0})", minute.ToString());

        return string.Format("({0} + {1})", minute.ToString(), plus.ToString());
    }
}